<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>chili wasm test</title>
    <style type="text/css">
        body {
            font-family: Arial, sans-serif;
        }

        #output {
            margin: 20px;
        }

        h2 {
            margin-top: 20px;
        }

        p {
            margin: 0;
            margin-left: 20px;
        }

        .failed {
            color: red;
        }

        .result {
            margin-top: 10px;
            font-weight: bold;
        }
    </style>
    </style>
    
</head>

<body>
    <div id="output"></div>
    <script type="text/javascript">
        function resultMessage(passed, failed, time) {
            let p = document.createElement('p');
            p.className = 'result';
            p.innerHTML = `Passed: ${passed}, Failed: ${failed}, Time: ${time}`;
            if (failed > 0) {
                p.className += ' failed';
            }
            return p;
        }

        function failedMessage(actual, expected) {
            let p = document.createElement('p');
            p.innerHTML = `Expected ${expected}, but got ${actual}`;
            p.className = 'failed';
            return p;
        }

        async function test(name, fn) {
            var passed = 0, failed = 0;
            const expect = (actual) => {
                return {
                    toBe(expected) {
                        if (actual !== expected) {
                            div.append(failedMessage(actual, expected));
                            failed++;
                        } else {
                            passed++;
                        }
                    }
                };
            };

            let div = document.createElement('div');
            div.className = 'test';
            output.append(div);
            let title = document.createElement('h2');
            div.innerHTML = name
            output.append(title);
            let start = performance.now();
            fn(expect);
            let end = performance.now();
            div.append(resultMessage(passed, failed, end - start));
        }

    </script>
    <script type="module">
        window.onload = async () => {
            let output = document.getElementById('output');
            let initWasm = await import('../build/target/debug/chili-wasm.js');
            let wasm = await initWasm.default();
            test("test face mesh", (expect) => {
                let location = { x: 0, y: 0, z: 0 };
                let direction = { x: 0, y: 0, z: 1 };
                let xDirection = { x: 1, y: 0, z: 0 };
                let ax3 = { location, direction, xDirection };
                let box = wasm.ShapeFactory.box(ax3, 1, 2, 3).shape;
                let mesh = new wasm.FaceMesher(box, 0.1);

                expect(mesh.getPosition().length).toBe(72);
                expect(mesh.getIndex().length).toBe(36);
                expect(mesh.getGroups().length).toBe(12);
                expect(mesh.getNormal().length).toBe(72);
                expect(mesh.getUV().length).toBe(48);
            })

            test("test edge mesh", (expect) => {
                let location = { x: 0, y: 0, z: 0 };
                let direction = { x: 0, y: 0, z: 1 };
                let xDirection = { x: 1, y: 0, z: 0 };
                let ax3 = { location, direction, xDirection };
                let box = wasm.ShapeFactory.box(ax3, 1, 1, 1).shape;
                let mesh = new wasm.EdgeMesher(box, 0.1);

                expect(mesh.getPosition().length).toBe(72);
                expect(mesh.getGroups().length).toBe(24);
            })

            test("test mesh 1000", (expect) => {
                let count = 0;
                for (let i = 0; i < 1000; i++) {
                    let location = { x: 0, y: 0, z: 0 };
                    let direction = { x: 0, y: 0, z: 1 };
                    let xDirection = { x: 1, y: 0, z: 0 };
                    let ax2 = {location, direction, xDirection};
                    let box = wasm.ShapeFactory.box(ax2, 10, 10, 10).shape;
                    let fm = new wasm.FaceMesher(box, 0.1);
                    let em = new wasm.EdgeMesher(box, 0.1);

                    count += new Float32Array(fm.getPosition()).length;
                    count += new Uint32Array(fm.getIndex()).length;
                    count += new Uint32Array(fm.getGroups()).length;
                    count += new Float32Array(fm.getNormal()).length;
                    count += new Float32Array(fm.getUV()).length;

                    box.delete();
                    fm.delete();
                    em.delete();
                }
                console.log(count);
                
            })

            test("test shape", (expect) => {
                let location = { x: 0, y: 0, z: 0 };
                let direction = { x: 0, y: 0, z: 1 };
                let xDirection = { x: 1, y: 0, z: 0 };
                let ax3 = { location, direction, xDirection };
                let box = wasm.ShapeFactory.box(ax3, 1, 1, 1).shape;
                let edges = wasm.Shape.findSubShapes(box, wasm.TopAbs_ShapeEnum.TopAbs_EDGE);
                expect(edges.length).toBe(12);
                expect(edges[0].shapeType()).toBe(wasm.TopAbs_ShapeEnum.TopAbs_EDGE);

                let curve = wasm.Edge.curve(wasm.TopoDS.edge(edges[0]));
                let newEdge = wasm.Edge.fromCurve(curve.get());
                expect(wasm.Edge.curveLength(newEdge)).toBe(1);

                let faces = wasm.Shape.findAncestor(box, edges[0], wasm.TopAbs_ShapeEnum.TopAbs_FACE);
                expect(faces.length).toBe(2);
                expect(faces[0].shapeType()).toBe(wasm.TopAbs_ShapeEnum.TopAbs_FACE)

                let faceEdges = wasm.Shape.findSubShapes(faces[0], wasm.TopAbs_ShapeEnum.TopAbs_EDGE);
                expect(faceEdges.length).toBe(4);

            })

        }
    </script>

</body>

</html>